package com.qmx.launcher.utils;

import android.content.Context;
import android.os.storage.StorageManager;
import android.util.Log;

import com.qmx.launcher.bean.StorageItemEntity;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chengxiangtong
 * @date 2018/11/28
 * version : 1.0
 * class describe：*
 */
public class UsbDeviceManager {
    private static final String TAG = "UsbDeviceManager";
    private static UsbDeviceManager usbManager;

    public static UsbDeviceManager getUsbDeviceManager() {
        if (usbManager != null) {
            return usbManager;
        }
        usbManager = new UsbDeviceManager();
        return usbManager;
    }


    public static List<StorageItemEntity> listAllStorage(Context context) {
        ArrayList<StorageItemEntity> storages = new ArrayList<StorageItemEntity>();
        StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);

        try {
            Class<?>[] paramClasses = {};
            Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);
            Object[] params = {};
            Object[] invokes = (Object[]) getVolumeList.invoke(storageManager, params);
            if (invokes != null) {
                StorageItemEntity info = null;
                Log.i("getVolumeList", "getVolumeList :" + getVolumeList.getName());
                for (int i = 0; i < invokes.length; i++) {
                    Object obj = invokes[i];
                    Method getPath = obj.getClass().getMethod("getPath", new Class[0]);
                    String path = (String) getPath.invoke(obj, new Object[0]);
                    info = new StorageItemEntity(path);

                    Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);
                    String state = (String) getVolumeState.invoke(storageManager, info.getUri());
                    info.setState(state);

                    Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);
                    info.setRemoveable(((Boolean) isRemovable.invoke(obj, new Object[0])).booleanValue());
                    Log.i("listAllStorage=", info.toString());
                    storages.add(info);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        storages.trimToSize();
        return storages;
    }

    public static List<StorageItemEntity> getAvaliableStorage(List<StorageItemEntity> infos) {
        List<StorageItemEntity> storages = new ArrayList<StorageItemEntity>();
        for (StorageItemEntity info : infos) {
            File file = new File(info.getUri());
            Log.i(TAG + "LWL", info.toString());
            Log.i(TAG + "LWL", "file" + file.exists() + file.isDirectory() + file.canWrite());
            if ((file.exists()) && (file.isDirectory())) {
                if (info.getState() != null) {
                    if (info.getState().equals("mounted") && info.isRemoveable()) {
                        storages.add(info);
                    }
                }
            }
        }
        return storages;
    }
}
